﻿@namespace AntSK.Pages.Setting.AIModel
@using AntSK.Domain.Repositories
@using AntSK.Models
@using AntSK.Domain.Model.Enum
@page "/setting/model/add"
@page "/setting/model/add/{ModelId}"
@page "/setting/model/addbypath/{ModelPath}"
@using AntSK.Services.Auth
@inherits AuthComponentBase
@using Microsoft.AspNetCore.Authorization
@attribute [Authorize(Roles = "AntSKAdmin")]

<PageContainer Title="新增模型">
    <ChildContent>
        <Card>
            <Form Model="@_aiModel"
                  Style="margin-top: 8px;"
                  OnFinish="HandleSubmit">
                <FormItem Label="模型描述" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                    <Input Placeholder="请输入模型描述" @bind-Value="@context.ModelDescription" />
                </FormItem>

                <FormItem Label="AI类型" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                    <EnumRadioGroup @bind-Value="context.AIType"></EnumRadioGroup>
                </FormItem>
                <FormItem Label="模型类型" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                    <RadioGroup @bind-Value="context.AIModelType">
                        <Radio RadioButton Value="@(AIModelType.Chat)">会话模型</Radio>
                        <Radio RadioButton Value="@(AIModelType.Embedding)">向量模型</Radio>
                    </RadioGroup>
                </FormItem>
                @if (context.AIModelType == AIModelType.Embedding)
                {
                    <FormItem Label="注意事项" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <b>请不要使用不同维度的向量模型，否则会导致无法向量存储</b>
                    </FormItem>
                }

                @if (context.AIType == AIType.AzureOpenAI)
                {
                    <FormItem Label="请求地址" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Input Placeholder="请输入请求地址" @bind-Value="@context.EndPoint" />
                    </FormItem>
                    <FormItem Label="部署名(模型名)" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Input Placeholder="请输入部署名" @bind-Value="@context.ModelName" />
                    </FormItem>
                    <FormItem Label="模型秘钥" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <InputPassword @bind-Value="@context.ModelKey" Placeholder="请输入模型秘钥" Size="@InputSize.Large" />
                    </FormItem>
                }
                @if (context.AIType == AIType.OpenAI)
                {
                    <FormItem Label="请求地址" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Input Placeholder="请输入请求地址 示例格式 http://ip:port/" @bind-Value="@context.EndPoint" />
                    </FormItem>
                    <FormItem Label="模型名称" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Input Placeholder="请输入模型名称" @bind-Value="@context.ModelName" />
                    </FormItem>
                    <FormItem Label="模型秘钥" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <InputPassword @bind-Value="@context.ModelKey" Placeholder="请输入模型秘钥" Size="@InputSize.Large" />
                    </FormItem>
                }
                @if (context.AIType == AIType.SparkDesk)
                {
                    <FormItem Label="APPID" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Input Placeholder="请输入APPID" @bind-Value="@context.EndPoint" />
                    </FormItem>
                    <FormItem Label="APIKey" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <Input Placeholder="请输入请输入APIKey" @bind-Value="@context.ModelName" />
                    </FormItem>
                    <FormItem Label="APISecret" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <InputPassword @bind-Value="@context.ModelKey" Placeholder="APISecret" Size="@InputSize.Large" />
                    </FormItem>
                }
                @if (context.AIType == AIType.LLamaSharp)
                {
                    <FormItem Label="模型路径" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
                        <InputGroup>
                            <AutoComplete Options="_modelFiles" Placeholder="请输入模型路径" @bind-Value="@context.ModelName" />
                            <Button OnClick="()=>_downloadModalVisible=true">从Haggingface下载</Button>
                        </InputGroup>

                    </FormItem>
                }
                @if (context.AIType == AIType.Mock)
                {
                }
                <FormItem Label=" " Style="margin-top:32px" WrapperCol="LayoutModel._submitFormLayout.WrapperCol">
                    <Button Type="primary" OnClick="HandleSubmit">
                        保存
                    </Button>
                    <Button OnClick="Back">
                        返回
                    </Button>
                </FormItem>
            </Form>
        </Card>
    </ChildContent>
</PageContainer>

<Modal @ref="_modal" Visible="_downloadModalVisible" Footer="null" Closable Title="模型下载" OnCancel="OnCancel" DestroyOnClose>
    <Flex Gap="10" Vertical>
        <InputGroup>
            <Input Disabled="_downloadStarted" Placeholder="请输入下载地址" @bind-Value="_downloadUrl" Style="width:80%"></Input>
            @if (!_downloadStarted)
            {
                <Button OnClick="StartDownload">开始</Button>
            }
            else
            {
                <Button OnClick="Stop">停止</Button>
            }
        </InputGroup>
        <AntDesign.Progress Percent="_downloadProgress"></AntDesign.Progress>

    </Flex>
</Modal>
